package com.study.student_chuji.demos.service.impl;

import com.study.student_chuji.demos.Mapper.ProductMapper;
import com.study.student_chuji.demos.pojo.Product;
import com.study.student_chuji.demos.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;

    @Override
    public List<Product> getProductList() {

        return productMapper.getProductList();
    }

    @Override
    public void deleteProductById(Long id) {
        productMapper.deleteProductById(id);
    }

    @Override
    public Product getProductById(Long id) {
        return productMapper.getProductById(id);
    }

    @Override
    public void addProduct(Product product) {
        Date now = new Date();
        product.setCreatedTime(now); // 新增时设置创建时间
        product.setUpdatedTime(now); // 新增时更新时间同步
        product.setStatus((byte)1); // 默认上架状态
        productMapper.insertProduct(product);
    }

    @Override
    public void updateProduct(Product product) {
        product.setUpdatedTime(new Date()); // 仅更新修改时间
        productMapper.updateProduct(product);
    }

    @Override
    public List<Product> searchProduct(String name, Integer min, Integer maxAmount, String type) {
        return productMapper.searchProduct(name,min,maxAmount,type);
    }
}
